<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商品')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: jasny-bootstrap-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">分类：</label>
                <div class="col-sm-8">
                    <select name="categoryId" class="form-control" required>
                        <option value="">请选择</option>
                        <option th:each="category : ${categoryList}" th:value="${category.id}" th:text="${category.title}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品编号：</label>
                <div class="col-sm-8">
                    <input name="productNo" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">内容：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="content">
                    <div class="summernote" id="content"></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">产品卖点：</label>
                <div class="col-sm-8">
                    <textarea name="usp" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">缩略图：</label>
                <div class="col-sm-8">
                    <div class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 400px; height: 350px;"></div>
                        <div>
                            <span class="btn btn-white btn-file"><span class="fileinput-new">选择图片</span>
                                <span class="fileinput-exists">更改</span>
                                <input id="img-file" type="file"></span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">产品规格：</label>
                <div class="col-sm-8">
                    <textarea name="specs" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">排序编号：</label>
                <div class="col-sm-8">
                    <input name="orderNumber" class="form-control" type="number" value="1">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">商品价格：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="number" min="0" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">原始价格：</label>
                <div class="col-sm-8">
                    <input name="originPrice" class="form-control" type="number" min="0">
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 用于放在小程序显示，划掉的价格</span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">限时优惠价：</label>
                <div class="col-sm-8">
                    <input name="limitedPrice" class="form-control" type="number" min="0">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">限时优惠截止时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="limitedTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box">
                        <div class="radio-box">
                            <input type="radio" id="status_1" name="status" value="1" checked="checked">
                            <label for="status_1">上架</label>
                        </div>

                        <div class="radio-box">
                            <input type="radio" id="status_2" name="status" value="2">
                            <label for="status_2">下架</label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">评论状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box">
                        <div class="radio-box">
                            <input type="radio" id="commentStatus_1" name="commentStatus" value="1" checked="checked">
                            <label for="commentStatus_1">允许</label>
                        </div>

                        <div class="radio-box">
                            <input type="radio" id="commentStatus_0" name="commentStatus" value="0">
                            <label for="commentStatus_0">不允许</label>
                        </div>
                    </div>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 默认允许评论</span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">访问量：</label>
                <div class="col-sm-8">
                    <input name="viewCount" class="form-control" type="number">
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">销售量：</label>
                <div class="col-sm-8">
                    <input name="salesCount" class="form-control" type="number">
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 用于放在前台显示</span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">供货商：</label>
                <div class="col-sm-8">
                    <select name="supplierId" class="form-control" required>
                        <option value="">-请选择-</option>
                        <option th:each="supplier : ${shopSupplierList}" th:value="${supplier.id}" th:text="${supplier.name}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">是否特价：</label>
                <div class="col-sm-8">
                    <div class="radio-box">
                        <input type="radio" id="isSpecial_0" name="isSpecial" value="0" checked="checked">
                        <label for="isSpecial_0">否</label>
                    </div>

                    <div class="radio-box">
                        <input type="radio" id="isSpecial_1" name="isSpecial" value="1">
                        <label for="isSpecial_1">是</label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否新品：</label>
                <div class="col-sm-8">
                    <div class="radio-box">
                        <input type="radio" id="isNew_0" name="isNew" value="0" checked="checked">
                        <label for="isSpecial_0">否</label>
                    </div>

                    <div class="radio-box">
                        <input type="radio" id="isNew_1" name="isNew" value="1">
                        <label for="isSpecial_1">是</label>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">剩余库存：</label>
                <div class="col-sm-8">
                    <input name="stock" class="form-control" type="number" min="0">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注信息：</label>
                <div class="col-sm-8">
                    <textarea name="remark" class="form-control"></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "shop/product"
        $("#form-product-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                //$.operate.save(prefix + "/add", $('#form-product-add').serialize());

                uploadFile(prefix + "/add", $('#form-product-add').serializeObject());
            }
        }

        $.fn.serializeObject = function()
        {
            var o = new FormData();
            var formArray = this.serializeArray();
            $.each(formArray, function() {

                o.append(this.name+'', (this.value || ''));
            });
            return o;
        };

        $("input[name='limitedTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });


        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                height : 350,
                dialogsInBody: true,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                            type: "post",
                            url: ctx + "common/upload/product",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    		    if (result.code == web_status.SUCCESS) {
                    		        $('#' + obj.id).summernote('insertImage', result.url);
                    		    } else {
                    		        $.modal.alertError(result.msg);
                    		    }
                    		},
                    		error: function(error) {
                    		    $.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });


        function uploadFile(url, formData) {
            var file = $('#img-file')[0].files[0];

            if(file != undefined){
                var fileSize = parseInt(file.size / 1024); //KB
                if(fileSize > 1024){
                    $.modal.alertWarning("选择的文件大小："+fileSize+"KB，请控制图片的大小在100KB - 1M");
                    return;
                }
            }
            formData.append('file', file);
            $.ajax({
                url: url,
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
</html>